﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.ServiceModel.Dispatcher;
using System.Text;

namespace WooCoo.DataPortal.Server.Hosts.WcfChannel
{
    /// <summary>
    /// 服务端消息调度检查,通过配置文件加入服务器终结点形为，以监听交互。
    /// </summary>
    public class ServiceDispatchMessageInspector : IDispatchMessageInspector
    {
        #region IDispatchMessageInspector 成员

        public object AfterReceiveRequest(ref System.ServiceModel.Channels.Message request, System.ServiceModel.IClientChannel channel, System.ServiceModel.InstanceContext instanceContext)
        {
            //int i = request.Headers.FindHeader("", "");
            //if (i < 0)
            //{
            //    //没有找到，外部需求进行验证,方式待后定
            //    //DataPermissionManager.Instance.Validate(context.UserName, context.Password);
            //}
            //var context = request.Headers.GetHeader<DataPortalContext>(i);
            //if (context != null)
            //{
            //    //if (context.Principal is Wcdf.Security.UnauthenticatedPrincipal)
            //    //{
            //    //    //ApplicationContext.ClientContext["LoginAuth"] = "true";
            //    //    var id = context.Principal.Identity as UnauthenticatedIdentity;
            //    //    DataPermissionManager.Instance.Validate(id.UserName, id.Password);
            //    //}
            //    //else if (ApplicationContext.ValidateUserNameType != "FirstLogin")
            //    //{
            //    //    var id = context.Principal.Identity as Wcdf.Security.WcdfIdentity;
            //    //    DataPermissionManager.Instance.Validate(id.UserInfo.UserName, id.UserInfo.Password);
            //    //}
            //    //ApplicationContext.SetContext(context);
            //}
            return null;
        }

        public void BeforeSendReply(ref System.ServiceModel.Channels.Message reply, object correlationState)
        {
            //ApplicationContext.ClearContext();
        }

        #endregion
    }
}
